//
//  SerialPhotoDetailViewController.m
//  XPhoto
//
//  Created by baboy on 2019/7/2.
//  Copyright © 2019 baboy. All rights reserved.
//

#import "SerialPostDetailViewController.h"
#import "BKIndicator.h"
#import "PhotoTableViewCell.h"
#import "PostAction.h"
#import "PostDetailViewController.h"
#import "Comment.h"
#import "UIScrollView+x.h"
#import <BKKit/BKDropMenu.h>
#import <BKKit/Theme.h>
#import <AFNetworking/UIImageView+AFNetworking.h>
#import "SysDict.h"

#define TABLE_VIEW_SECTION_COVER_INDEX  0
#define TABLE_VIEW_SECTION_MAP_INDEX    1
#define TABLE_VIEW_SECTION_POST_INDEX   2


#define COVER_HEIGHT    180

@interface SerialPostDetailViewController ()<PhotoTableViewCellDelegate, SerialGroupViewDelegate, PhotoMapViewDelegate, BKCommentViewDelegate, BKDropMenuDelegate,UIScrollViewDelegate>
@property(nonatomic, assign) NSInteger serialType;
@property(nonatomic, assign) BOOL defaultNavBarHidden;
@end

@implementation SerialPostDetailViewController

- (id)init{
    if (self = [super initWithNibName:NSStringFromClass([self class]) bundle:nil]){
        self.hidesBottomBarWhenPushed = YES;
    }
    return self;
}
- (void)setSerialType:(NSInteger)serialType{
    _serialType = serialType;
    UIView *previousView = self.scrollView;
    SerialPostDateView *scrollView = nil;
    if (serialType == SERIAL_TYPE_LOCATION) {
        scrollView = [SerialPostLocationView view];
    }
    if (serialType == SERIAL_TYPE_TAG) {
        scrollView = [SerialPostTagView view];
    }
    if (!scrollView) {
        scrollView = [SerialPostDateView view];
    }
    CGRect frame = CGRectMake(0, self.view.safeAreaInsets.top, self.view.bounds.size.width, self.view.bounds.size.height - self.view.safeAreaInsets.top - self.view.safeAreaInsets.bottom);
    if (previousView) {
        frame.origin.x = -self.view.bounds.size.width;
    }
    scrollView.frame = frame;
    scrollView.postSerial = self.postSerial;
    scrollView.actionDelegate = self;
    scrollView.delegate = self;
    scrollView.navBar = self.navBar;
    
    
    [self.view insertSubview:scrollView aboveSubview:self.coverImageView];
    self.scrollView = scrollView;
    if (previousView) {
        [UIView animateWithDuration:0.5
                         animations:^{
            CGRect frame = self.scrollView.frame;
            frame.origin.x = 0;
            scrollView.frame = frame;
        
        }
                         completion:^(BOOL finished) {
            
        }];
        [UIView animateWithDuration:0.5
                         animations:^{
            CGRect frame = previousView.frame;
            frame.origin.x = self.view.bounds.size.width;
            previousView.frame = frame;
        
        }
                         completion:^(BOOL finished) {
            [previousView removeFromSuperview];
            
        }];
    }
    [scrollView reload];
    [self resizeCoverImageView];
}
- (void)viewDidLoad {
    [super viewDidLoad];
//    self.coverImageView.layer.borderColor = [UIColor redColor].CGColor;
//    self.coverImageView.layer.borderWidth = 5;
    self.navBarTitleLabel.text = self.postSerial.title;
    [self.coverImageView setImageWithURL:[NSURL URLWithString:self.postSerial.cover] placeholderImage:nil];
    
    self.serialType = self.postSerial.type;
//    [self searchWithOffset:0];
    // Do any additional setup after loading the view from its nib.
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.defaultNavBarHidden = self.navigationController.navigationBar.hidden;
    self.navigationController.navigationBar.hidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.hidden = self.defaultNavBarHidden;
}
- (void)viewSafeAreaInsetsDidChange{
    [super viewSafeAreaInsetsDidChange];
    [self resizeCoverImageView];
    CGRect frame = CGRectMake(0, self.view.safeAreaInsets.top, self.view.bounds.size.width, self.view.bounds.size.height - self.view.safeAreaInsets.top - self.view.safeAreaInsets.bottom);
    self.scrollView.frame = frame;
}
- (void)resizeCoverImageView{
    CGFloat coverImageViewHeight = self.view.safeAreaInsets.top - self.scrollView.contentOffset.y + self.scrollView.contentInset.top+ COVER_HEIGHT;
    
//    DLOG(@"resizeCoverImageView|coverImageViewHeight:%f,%@,%@,%@", coverImageViewHeight,NSStringFromCGPoint(self.scrollView.contentOffset),NSStringFromUIEdgeInsets(self.view.safeAreaInsets),NSStringFromUIEdgeInsets(self.scrollView.safeAreaInsets) );
    CGRect coverImageViewFrame = self.coverImageView.frame;
    coverImageViewHeight = MAX(coverImageViewHeight, COVER_HEIGHT/3);
    if (coverImageViewHeight != coverImageViewFrame.size.height) {
        coverImageViewFrame.size.height = coverImageViewHeight;
        self.coverImageView.frame = coverImageViewFrame;
    }
}
#pragma UISCrollView Delegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [self.scrollView scrollViewDidScroll:scrollView];
    if (scrollView == self.scrollView) {
        DLOG(@"scrollView contentOffset:%@%@%@", NSStringFromCGPoint(scrollView.contentOffset), NSStringFromUIEdgeInsets(self.view.safeAreaInsets), NSStringFromUIEdgeInsets(self.scrollView.safeAreaInsets));
        
        [self resizeCoverImageView];
    }
    
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    [self.scrollView scrollViewDidEndDragging:scrollView willDecelerate:decelerate];
}
#pragma BKDropMenuDelegate
- (void)dropMenu:(BKDropMenu *)dropMenu didSelectItem:(BKDropMenuItem *)item{
    DLOG(@"%@", item);
    self.serialType = [item.data intValue];
}
- (void)didClickedSerialGroupTitleButton:(UIButton *)button{
    SysDict *serialDict = [SysDict dictForKey:SYS_DICT_SERIAL_TYPE_KEY];
    if (serialDict && serialDict.items) {
        NSMutableArray *menus = [NSMutableArray array];
        for (SysDictItem *item in serialDict.items) {
            BKDropMenuItem *menu = [[BKDropMenuItem alloc] initWithTitle:item.title data:item.value];
            menu.enabled = self.serialType != [item.value intValue];
            [menus addObject:menu];
        }
        BKDropMenu *dropMenu = [BKDropMenu dropMenuWithItems:menus anchorView:button delegate:self];
        [dropMenu show];
        
    }
}
- (void)serialTypeViewDidNotSupportCurrentSerialType:(id)view{
    if ([view isKindOfClass:[SerialPostTagView class]]) {
        self.serialType = SERIAL_TYPE_DATE;
    }
}
-(IBAction)shareAction:(id)sender{
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
